﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DAO;
using System.Globalization;
using System.IO;

using COMExcel = Microsoft.Office.Interop.Excel;
using System.Reflection;

 namespace TiDiSun
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

        public int timer = 0;

        #region "Thuộc tính"

        private double _tongCongSuatTieuThu = 0; //Biến này lưu tổng công suất tiêu thụ điện
        private double _congSuatCuaPinTaoRa;//Biến này lưu công suất pin tạo ra.

        #endregion

        #region "Phương thức"


        #endregion

        double CSHH, TongCS = 0, tam = 0;
        bool co = false;
        bool flag,flag1 = false;
        double TongTienTatCa = 0;
        public void btnChonPin_Click(object sender, EventArgs e)
        {
            frmLoaiPin frm = new frmLoaiPin();
            frm.Show();
        }
        public void btnAdd_Click_1(object sender, EventArgs e)
        {
            try
            {
               
                string tenSP = txtTenSP.Text;
                double CS = double.Parse(txtCS.Text);
                int thoiGian = int.Parse(txtThoiGian.Text);
                int soluong = int.Parse(txtSoLuong.Text);
                if (txtCSHaoHut.Text == "")
                    CSHH = 0;
                else
                    CSHH = double.Parse(txtCSHaoHut.Text);
                TongCS = (CS * thoiGian * soluong + CSHH);
                tam = tam + TongCS;
                dgThemSP.Rows.Add(tenSP, CS, thoiGian, soluong, CSHH, TongCS);
                gvThietBi.Rows.Add(tenSP, CS, thoiGian, soluong, CSHH, TongCS);

                // lay thong tin de xuat excel
                HoaDon_ThietBi thietbi = new HoaDon_ThietBi();
                txtTongCS.Text = tam.ToString();
                //lbTongCS.Text = tam.ToString() + " (W)";
                txtTenSP.Text = "";
                txtCS.Text = "";
                txtThoiGian.Text = "";
                txtSoLuong.Text = "";
                txtCSHaoHut.Text = "";
                frmLoaiPin frm = new frmLoaiPin();
                frm.Tag = TongCS;

                CapNhatCongSuatTieuThu();
                
                if (txtSoGioNang.Text != "")
                    btnTinh_Click(sender, e);
                gbTinhCongSuatPin.Enabled = true;
                flag = true;
            }
            catch (Exception)
            {
                MessageBox.Show("Hãy Nhập Đủ Thông Tin");
            }
        }



        //Bien nay luu trang thai da cap nhat dong hay chua
        public static bool flag_CapNhat = false;
        public void Form1_Load(object sender, EventArgs e)
        {
            
            //Ban đầu chưa tính công suất tiêu thụ, ẩn các form tính công suất pin
            gbTinhCongSuatPin.Enabled = false;
            
            txtTenSP.Focus();
            cmbCachDung.SelectedIndex = 0;
            load_pin();

            
            // Sản phẩm---------------------------------------------------------
            LoaiPinBUS loaipin = new LoaiPinBUS();
            cbbloaipin_sanpham.DataSource = loaipin.load_loaiPin();
            cbbloaipin_sanpham.ValueMember = "MaLoaiPin";
            cbbloaipin_sanpham.DisplayMember = "TenLoai";

            // load pin
            search_pin(txtSearchPin.Text);
            // load mach sac
            search_machsac(txtSearchMachSac.Text);
            // load ac quy
            search_sanpham_acquy(txtSearch_sanpham_acquy.Text);
            //load bo chuyen doi dien
            string text = txtSeacrh_sanpham_bochuyendoidien.Text;
            gvSanpham_bochuyendoi.DataSource = search_sanpham_bochuyendoidien(text);


            // hieu ung hinh anh button
            this.button5.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.button5.BackgroundImageLayout = ImageLayout.Stretch;

            this.button3.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.button3.BackgroundImageLayout = ImageLayout.Stretch;

            this.button2.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.button2.BackgroundImageLayout = ImageLayout.Stretch;

            this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.button1.BackgroundImageLayout = ImageLayout.Stretch;

            this.btnCapNhapThamSo.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.btnCapNhapThamSo.BackgroundImageLayout = ImageLayout.Stretch;


            this.btnCapNhapThamSo.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.btnCapNhapThamSo.BackgroundImageLayout = ImageLayout.Stretch;


            this.btnChonPin.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.btnChonPin.BackgroundImageLayout = ImageLayout.Stretch;


            this.btnChonMachSac.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.btnChonMachSac.BackgroundImageLayout = ImageLayout.Stretch;


            this.btnChonBoKichDien.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.btnChonBoKichDien.BackgroundImageLayout = ImageLayout.Stretch;

            this.button4.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.button4.BackgroundImageLayout = ImageLayout.Stretch;
            
        }


        public void load_pin()
        {
            LoaiPinBUS loaipin = new LoaiPinBUS();
            cmbDongPin.DataSource = loaipin.load_loaiPin();
            cmbDongPin.ValueMember = "MaLoaiPin";
            cmbDongPin.DisplayMember = "TenLoai";
        }

        public void cmbCachDung_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cmbCachDung.SelectedIndex == 1)
            {
                pnNgayDuPhong.Enabled = true;

            }
            else
                pnNgayDuPhong.Enabled = false;
        }
        double tongcspin = 0;
        double TienPin = 0;
        public void btnTinh_Click(object sender, EventArgs e)
        {
            if (cmbCachDung.SelectedIndex == 0 && txtSoGioNang.Text == "" )
            {
                MessageBox.Show("Nhập đầy đủ thông tin");

            }
            if (cmbCachDung.SelectedIndex == 1 && txtNgayDuPhong.Text == "")
            {
                MessageBox.Show("Nhập đầy đủ thông tin");
            }
            else
            {
                int sogionang = 0;
                int songayduphong = 0;
                sogionang = int.Parse(txtSoGioNang.Text);
                if (pnNgayDuPhong.Enabled == false)
                {
                    tongcspin = _tongCongSuatTieuThu / sogionang;
                }
                else
                {
                        songayduphong = int.Parse(txtNgayDuPhong.Text);
                        tongcspin = _tongCongSuatTieuThu / songayduphong / sogionang;
                    

                }
                txtTongCSPin.Text = Math.Round(tongcspin, 0).ToString();
                //lbCSPin.Text = Math.Round(tongcspin, 0).ToString() + " (Wh)";
                _congSuatCuaPinTaoRa = tongcspin;
                TienPin = double.Parse(txtGiaPin.Text);
                txtTienPin.Text = (tongcspin * TienPin).ToString("###,###,##0.", CultureInfo.InvariantCulture).TrimStart().Replace(".", ",");
                flag1 = true;

               
            }
           
            
           
        }
        
        public void cmbDongPin_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (cmbDongPin.SelectedIndex == 0)
            //{
            //    txtGiaPin.Text = "29.000";
                
            //}
            //else
            //{
            //    txtGiaPin.Text = "39.000";
                
            //}
            //
            if (cmbDongPin.SelectedValue.ToString() != "DTO.LoaiPinDTO")
            {
                int MaLoaiPin = Convert.ToInt32(cmbDongPin.SelectedValue.ToString());
                LoaiPinBUS loaipin = new LoaiPinBUS();
                txtGiaPin.Text = loaipin.Load_GiaPin(MaLoaiPin).ToString("###,###,##0.", CultureInfo.InvariantCulture).TrimStart().Replace(".", ",");
                TienPin = double.Parse(txtGiaPin.Text);
                txtTienPin.Text = (tongcspin * TienPin).ToString("###,###,##0.", CultureInfo.InvariantCulture).TrimStart().Replace(".", ",");
                
                

            }


            
        }

        public void button3_Click(object sender, EventArgs e)
        {

        }

        private void btnChonAcquy_Click(object sender, EventArgs e)
        {
            frmChonAcquy  FrmChonAcQuy = new frmChonAcquy();
            FrmChonAcQuy.Tag = txtTongCS.Text;
            FrmChonAcQuy.Show();
            
            
        }

        private void txtCS_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Mời bạn nhập số");

            }
           
        }

        private void txtThoiGian_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ Được Nhập số");

            }
           
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ Được Nhập số");

            }
           
        }

        private void txtCSHaoHut_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ Được Nhập số");

            }

        }

        private void txtNgayDuPhong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ Được Nhập số");

            }
           
        }

        private void txtSoGioNang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ Được Nhập số");

            }
           
        }

       

        private void button4_Click(object sender, EventArgs e)
        {
            if (co)
            {
                frmChonAcquy frm = new frmChonAcquy();

                //biến này lưu các tham số cần truyền qua form chọn acquy bao gồm:
                //cách dùng + công suất của pin cần tạo ra.
                List<string> _arrThamSo = new List<string> { cmbCachDung.SelectedItem.ToString(), _congSuatCuaPinTaoRa.ToString(), txtNgayDuPhong.Text };
                frm.Tag = _arrThamSo;
                frm.ShowDialog();

                DataTable dt = new DataTable();
                dt = HoaDonHelper.dt;
                TongTienTatCa += HoaDonHelper.tongtien;
                lbTienAcQuy.Text = HoaDonHelper.tongtien.ToString("###,###,##0.", CultureInfo.InvariantCulture).TrimStart().Replace(".", ",") + " VNĐ";
                gvHD_ACQuy.DataSource = dt;
                dt = null;
            }
            else
                MessageBox.Show("Hãy SAVE Trước Khi Chọn Chức Năng!!!!!!");
        }


        private void btnFinish_Click(object sender, EventArgs e)
        {
            

        }



        //Hàm này kiểm tra trạng thái cần để bật nút hoàn thành
        //Bật khi số dòng dữ liệu > 1 (1 dong newIndex)
        private bool dudieukien() {
            bool result = false;
            if (dgThemSP.Rows.Count > 0) {
                result = true;
            }
            return result;
        } 

        //Hàm này tính toán lại tổng công suất tiêu thụ
        private void CapNhatCongSuatTieuThu() {
            double congSuat = 0;
            if (dgThemSP.Rows.Count > 0)
            {
                foreach (DataGridViewRow r in dgThemSP.Rows)
                {
                    if (r.Index != dgThemSP.NewRowIndex)
                    {
                        congSuat += double.Parse(r.Cells[5].Value.ToString());
                    }
                }
            }
            _tongCongSuatTieuThu = congSuat;
            txtTongCS.Text = congSuat.ToString();
        }


        private void toolStrip4_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }









// Quan ly pin----------------------------------------------------------------------------------------------------------------------------------

        private void txtKichThuocPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void txtVonMaxPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void txtVonMinPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void txtAmpeMaxPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void txtAmpeMinPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void txtMaPin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void btnSearchpin_Click(object sender, EventArgs e)
        {
            search_pin(txtSearchPin.Text);
        }

        private void search_pin(string text)
        {
            List<SanPham_PinDTO> listPin = new List<SanPham_PinDTO>();
            PinBUS pinBus = new PinBUS();

            listPin = pinBus.search_pin(text);
            gvPin.DataSource = listPin;
        }

        private void txtSearchPin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                search_pin(txtSearchPin.Text);
            }
        }

        private void SanPham_StatusThemPin()
        {
            if (txtKichThuocPin.Text == "Hãy nhập")
            {
                txtKichThuocPin.Text = "Hãy nhập";
                txtKichThuocPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtVonMaxPin.Text == "Hãy nhập")
            {
                txtVonMaxPin.Text = "Hãy nhập";
                txtVonMaxPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtVonMinPin.Text == "Hãy nhập")
            {
                txtVonMinPin.Text = "Hãy nhập";
                txtVonMinPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtAmpeMaxPin.Text == "Hãy nhập")
            {
                txtAmpeMaxPin.Text = "Hãy nhập";
                txtAmpeMaxPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtAmpeMinPin.Text == "Hãy nhập")
            {
                txtAmpeMinPin.Text = "Hãy nhập";
                txtAmpeMinPin.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void btnThemPin_Click(object sender, EventArgs e)
        {
            if (txtKichThuocPin.Text == "Hãy nhập" || txtVonMaxPin.Text == "Hãy nhập" || txtVonMinPin.Text == "Hãy nhập" || txtAmpeMaxPin.Text == "Hãy nhập" || txtAmpeMinPin.Text == "Hãy nhập")
            {
                SanPham_StatusThemPin();
                return;
            }

            SanPham_DaTa_Pin pin = new SanPham_DaTa_Pin();
            pin.KichThuoc = txtKichThuocPin.Text;
            pin.VMax = float.Parse(txtVonMaxPin.Text);
            pin.VMin = float.Parse(txtVonMinPin.Text);
            pin.AMax = float.Parse(txtAmpeMaxPin.Text);
            pin.AMin = float.Parse(txtAmpeMinPin.Text);
            pin.LoaiPin = int.Parse(cbbloaipin_sanpham.SelectedValue.ToString());

            try
            {
                PinBUS pinBus = new PinBUS();
                pinBus.Insert_Sanpham_Pin(pin);

                // khởi tạo lại---------------------------------------
                txtKichThuocPin.Text = "Hãy nhập"; 
                txtKichThuocPin.ForeColor = System.Drawing.Color.DarkGray;
                txtVonMaxPin.Text = "Hãy nhập";
                txtVonMaxPin.ForeColor = System.Drawing.Color.DarkGray;
                txtVonMinPin.Text = "Hãy nhập";
                txtVonMinPin.ForeColor = System.Drawing.Color.DarkGray;
                txtAmpeMaxPin.Text = "Hãy nhập";
                txtAmpeMaxPin.ForeColor = System.Drawing.Color.DarkGray;
                txtAmpeMinPin.Text = "Hãy nhập";
                txtAmpeMinPin.ForeColor = System.Drawing.Color.DarkGray;

                search_pin(txtSearchPin.Text);
            }
            catch { }
        }


        // sự kiện click------------------------------------------------------------------------------------------------------
        private void txtKichThuocPin_Click(object sender, EventArgs e)
        {
            if (txtKichThuocPin.Text == "Hãy nhập")
            {
                txtKichThuocPin.Text = "";
                txtKichThuocPin.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtVonMaxPin_Click(object sender, EventArgs e)
        {
            if (txtVonMaxPin.Text == "Hãy nhập")
            {
                txtVonMaxPin.Text = "";
                txtVonMaxPin.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtVonMinPin_Click(object sender, EventArgs e)
        {
            if (txtVonMinPin.Text == "Hãy nhập")
            {
                txtVonMinPin.Text = "";
                txtVonMinPin.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtAmpeMaxPin_Click(object sender, EventArgs e)
        {
            if (txtAmpeMaxPin.Text == "Hãy nhập")
            {
                txtAmpeMaxPin.Text = "";
                txtAmpeMaxPin.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtAmpeMinPin_Click(object sender, EventArgs e)
        {
            if (txtAmpeMinPin.Text == "Hãy nhập")
            {
                txtAmpeMinPin.Text = "";
                txtAmpeMinPin.ForeColor = System.Drawing.Color.Black;
            }
        }



        // sự kiện leave-----------------------------------------------------------------------------------------------
        private void txtKichThuocPin_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtKichThuocPin.Text))
            {
                txtKichThuocPin.Text = "Hãy nhập";
                txtKichThuocPin.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtVonMaxPin_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtVonMaxPin.Text))
            {
                txtVonMaxPin.Text = "Hãy nhập";
                txtVonMaxPin.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtVonMinPin_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtVonMinPin.Text))
            {
                txtVonMinPin.Text = "Hãy nhập";
                txtVonMinPin.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtAmpeMaxPin_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAmpeMaxPin.Text))
            {
                txtAmpeMaxPin.Text = "Hãy nhập";
                txtAmpeMaxPin.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtAmpeMinPin_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAmpeMinPin.Text))
            {
                txtAmpeMinPin.Text = "Hãy nhập";
                txtAmpeMinPin.ForeColor = System.Drawing.Color.DarkGray;
            }
        }


        // sự kiện textChanged-------------------------------------------------------------------------------
        private void txtKichThuocPin_TextChanged(object sender, EventArgs e)
        {
            txtKichThuocPin.ForeColor = System.Drawing.Color.Black;
        }

        private void txtVonMaxPin_TextChanged(object sender, EventArgs e)
        {
            txtVonMaxPin.ForeColor = System.Drawing.Color.Black;
        }

        private void txtVonMinPin_TextChanged(object sender, EventArgs e)
        {
            txtVonMinPin.ForeColor = System.Drawing.Color.Black;
        }

        private void txtAmpeMaxPin_TextChanged(object sender, EventArgs e)
        {
            txtAmpeMaxPin.ForeColor = System.Drawing.Color.Black;
        }

        private void txtAmpeMinPin_TextChanged(object sender, EventArgs e)
        {
            txtAmpeMinPin.ForeColor = System.Drawing.Color.Black;
        }

        private void gvPin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (MessageBox.Show("Bạn coa muốn xoa Pin này??", "Thông Bóa", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    PinBUS pinBus = new PinBUS();
                    int id = int.Parse(gvPin.CurrentRow.Cells["id"].Value.ToString());

                    pinBus.Delete_SanPham_Pin(id);
                    search_pin(txtSearchPin.Text);
                }
            }
        }

        private void gvPin_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int id = int.Parse(gvPin.CurrentRow.Cells["id"].Value.ToString());

            if (e.ColumnIndex == 9)
            {
                if (MessageBox.Show("Bạn coa muốn xoa Pin này??", "Thông Bóa", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    PinBUS pinBus = new PinBUS();

                    pinBus.Delete_SanPham_Pin(id);
                    search_pin(txtSearchPin.Text);
                }
            }

            if (e.ColumnIndex == 8)
            {
                Update_Sanpham_Pin fr = new Update_Sanpham_Pin();
                fr.Tag = id;
                fr.ShowDialog();
                search_pin(txtSearchPin.Text);
            }
        }



        // sản phẩm: mạch sạc---------------------------------------------------------------------------
        private void btnSearch_MacSac_Click(object sender, EventArgs e)
        {
            try
            {
                search_machsac(txtSearchMachSac.Text);
            }
            catch { }
        }

        private void search_machsac(string text)
        {
            List<SanPham_MachSacDTO> listMachSac = new List<SanPham_MachSacDTO>();

            listMachSac = MachSacBUS.search_machsac(text);
            gvMacSac.DataSource = listMachSac;
        }

        private void txtSearchMachSac_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                search_machsac(txtSearchMachSac.Text);
            }
        }

        private void txtMachSac_GiaTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private bool SanPham_StatusThemMachSac()
        {
            if (txtMachSac_HieuSuat1.Text == "Hãy nhập")
            {
                txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.Red;
                return false;
            }
            if (txtMachSac_HieuSuat2.Text == "Hãy nhập")
            {
                txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.Red;
                return false;
            }
            if (txtMachSac_Ample.Text == "Hãy nhập")
            {
                txtMachSac_Ample.ForeColor = System.Drawing.Color.Red;
                return false;
            }
            if (txtMachSac_GiaTien.Text == "Hãy nhập")
            {
                txtMachSac_GiaTien.ForeColor = System.Drawing.Color.Red;
                return false;
            }

            return true;
        }

        private void btnThemMachSac_Click(object sender, EventArgs e)
        {
            if (SanPham_StatusThemMachSac())
            {
                try
                {
                    SanPham_MachSacDTO mSac = new SanPham_MachSacDTO();
                    mSac.hieusuat1 = txtMachSac_HieuSuat1.Text;
                    mSac.hieusuat2 = txtMachSac_HieuSuat2.Text;
                    mSac.ample = txtMachSac_Ample.Text;
                    mSac.giatien = float.Parse(txtMachSac_GiaTien.Text);

                    MachSacBUS.Insert_sanpham_machsac(mSac);
                    // khởi tạo lại--------------------------------------
                    txtMachSac_GiaTien.Text = "Hãy nhập";
                    txtMachSac_GiaTien.ForeColor = System.Drawing.Color.DarkGray;
                    txtMachSac_Ample.Text = "Hãy nhập";
                    txtMachSac_Ample.ForeColor = System.Drawing.Color.DarkGray;
                    txtMachSac_HieuSuat2.Text = "Hãy nhập";
                    txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.DarkGray;
                    txtMachSac_HieuSuat1.Text = "";
                    txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.Black;

                    // load lai from
                    search_machsac(txtSearchMachSac.Text);
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }


        // event text changed-----------------------------------------------------------------
        private void txtMachSac_HieuSuat1_TextChanged(object sender, EventArgs e)
        {
            txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.Black;
        }

        private void txtMachSac_HieuSuat2_TextChanged(object sender, EventArgs e)
        {
            txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.Black;
        }

        private void txtMachSac_Ample_TextChanged(object sender, EventArgs e)
        {
            txtMachSac_Ample.ForeColor = System.Drawing.Color.Black;
        }

        private void txtMachSac_GiaTien_TextChanged(object sender, EventArgs e)
        {
            txtMachSac_GiaTien.ForeColor = System.Drawing.Color.Black;
        }


        // event click-----------------------------------------------------------------
        private void txtMachSac_HieuSuat1_Click(object sender, EventArgs e)
        {
            if (txtMachSac_HieuSuat1.Text == "Hãy nhập")
            {
                txtMachSac_HieuSuat1.Text = "";
                txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtMachSac_HieuSuat2_Click(object sender, EventArgs e)
        {
            if (txtMachSac_HieuSuat2.Text == "Hãy nhập")
            {
                txtMachSac_HieuSuat2.Text = "";
                txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtMachSac_Ample_Click(object sender, EventArgs e)
        {
            if (txtMachSac_Ample.Text == "Hãy nhập")
            {
                txtMachSac_Ample.Text = "";
                txtMachSac_Ample.ForeColor = System.Drawing.Color.Black;
            }
        }

        private void txtMachSac_GiaTien_Click(object sender, EventArgs e)
        {
            if (txtMachSac_GiaTien.Text == "Hãy nhập")
            {
                txtMachSac_GiaTien.Text = "";
                txtMachSac_GiaTien.ForeColor = System.Drawing.Color.Black;
            }
        }


        // event leave---------------------------------------------------------------
        private void txtMachSac_HieuSuat1_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMachSac_HieuSuat1.Text))
            {
                txtMachSac_HieuSuat1.Text = "Hãy nhập";
                txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtMachSac_HieuSuat2_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMachSac_HieuSuat2.Text))
            {
                txtMachSac_HieuSuat2.Text = "Hãy nhập";
                txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtMachSac_Ample_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMachSac_Ample.Text))
            {
                txtMachSac_Ample.Text = "Hãy nhập";
                txtMachSac_Ample.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtMachSac_GiaTien_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMachSac_GiaTien.Text))
            {
                txtMachSac_GiaTien.Text = "Hãy nhập";
                txtMachSac_GiaTien.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void gvMacSac_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if ((MessageBox.Show("Bạn muốn xóa mạch sạc nay??", "Thông Báo", MessageBoxButtons.YesNo)) == DialogResult.Yes)
                {
                    try
                    {
                        int id = int.Parse(gvMacSac.CurrentRow.Cells["MaMachSac"].Value.ToString());
                        MachSacBUS.Delete_sanpham_machdsac(id);
                        search_machsac(txtSearchMachSac.Text);
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message); }
                }
            }
        }

        private void gvMacSac_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int id = int.Parse(gvMacSac.CurrentRow.Cells["MaMachSac"].Value.ToString());

                // xoa mach sac--------------------------------------------------------------
                if (e.ColumnIndex == 1)
                {
                    if ((MessageBox.Show("Bạn muốn xóa mạch sạc nay??", "Thông Báo", MessageBoxButtons.YesNo)) == DialogResult.Yes)
                    {
                        MachSacBUS.Delete_sanpham_machdsac(id);
                        search_machsac(txtSearchMachSac.Text);
                    }
                }

                // chinh sua mach sac---------------------------------------------------------
                if (e.ColumnIndex == 0)
                {
                    Update_SanPham_MachSac fr = new Update_SanPham_MachSac();
                    fr.Tag = id;
                    fr.ShowDialog();

                    search_machsac(txtSearchMachSac.Text);
                }

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }



// sam pham Ac Quy---------------------------------------------------------------------------------------------------------------- 

        private void btnSearch_sanpham_acquy_Click(object sender, EventArgs e)
        {
            try
            {
                search_sanpham_acquy(txtSearch_sanpham_acquy.Text);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void search_sanpham_acquy(string text)
        {
            gvSanPham_Acquy.DataSource = AcQuyBUS.search_sanpham_acquy(text);
        }

        private void txtSearch_sanpham_acquy_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    search_sanpham_acquy(txtSearch_sanpham_acquy.Text);
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }

        private bool SanPham_StatusThemAcQuy()
        {
            if (txtAcquy_tensanpham.Text == "Hãy nhập")
            {
                txtAcquy_tensanpham.ForeColor = System.Drawing.Color.Red;
                return false;
            }
            if (txtAcquy_soAH.Text == "Hãy nhập")
            {
                txtAcquy_soAH.ForeColor = System.Drawing.Color.Red;
                return false;
            }
            if (txtAcquy_giatien.Text == "Hãy nhập")
            {
                txtAcquy_giatien.ForeColor = System.Drawing.Color.Red;
                return false;
            }

            return true;
        }

        private void btnSanPham_ThemAcquy_Click(object sender, EventArgs e)
        {
            if (SanPham_StatusThemAcQuy())
            {
                SanPham_AcQuy_DTO acquy = new SanPham_AcQuy_DTO();
                acquy.tensanpham = txtAcquy_tensanpham.Text;
                acquy.soAH = float.Parse(txtAcquy_soAH.Text);
                acquy.giatien = float.Parse(txtAcquy_giatien.Text);

                try
                {
                    AcQuyBUS.Insert_Sanpham_Acquy(acquy);

                    // khởi tao lai
                    txtAcquy_tensanpham.Text = "Hãy nhập";
                    txtAcquy_soAH.Text = "Hãy nhập";
                    txtAcquy_giatien.Text = "Hãy nhập";
                    txtAcquy_tensanpham.ForeColor = System.Drawing.Color.DarkGray;
                    txtAcquy_soAH.ForeColor = System.Drawing.Color.DarkGray;
                    txtAcquy_giatien.ForeColor = System.Drawing.Color.DarkGray;

                    // load lai
                    search_sanpham_acquy(txtSearch_sanpham_acquy.Text);
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }

        private void txtAcquy_soAH_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void txtAcquy_giatien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }


        // event text changed------------------------------------------------------------
        private void txtAcquy_tensanpham_TextChanged(object sender, EventArgs e)
        {
            txtAcquy_tensanpham.ForeColor = System.Drawing.Color.Black;
        }

        private void txtAcquy_soAH_TextChanged(object sender, EventArgs e)
        {
            txtAcquy_soAH.ForeColor = System.Drawing.Color.Black;
        }

        private void txtAcquy_giatien_TextChanged(object sender, EventArgs e)
        {
            txtAcquy_giatien.ForeColor = System.Drawing.Color.Black;
        }


        // event leave-------------------------------------------------------------
        private void txtAcquy_tensanpham_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAcquy_tensanpham.Text))
            {
                txtAcquy_tensanpham.Text = "Hãy nhập";
                txtAcquy_tensanpham.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtAcquy_soAH_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAcquy_soAH.Text))
            {
                txtAcquy_soAH.Text = "Hãy nhập";
                txtAcquy_soAH.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtAcquy_giatien_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAcquy_giatien.Text))
            {
                txtAcquy_giatien.Text = "Hãy nhập";
                txtAcquy_giatien.ForeColor = System.Drawing.Color.DarkGray;
            }
        }


        // event click------------------------------------------------------------
        private void txtAcquy_tensanpham_Click(object sender, EventArgs e)
        {
            if (txtAcquy_tensanpham.Text == "Hãy nhập")
                txtAcquy_tensanpham.Text = "";
        }

        private void txtAcquy_soAH_Click(object sender, EventArgs e)
        {
            if (txtAcquy_soAH.Text == "Hãy nhập")
                txtAcquy_soAH.Text = "";
        }

        private void txtAcquy_giatien_Click(object sender, EventArgs e)
        {
            if (txtAcquy_giatien.Text == "Hãy nhập")
                txtAcquy_giatien.Text = "";
        }

        private void gvSanPham_Acquy_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if ((MessageBox.Show("Bạn muốn xóa ac quy này ??", "Thông Báo", MessageBoxButtons.YesNo)) == DialogResult.Yes)
                {
                    try
                    {
                        int id = int.Parse(gvSanPham_Acquy.CurrentRow.Cells["clSanpham_Acquy_id"].Value.ToString());
                        AcQuyBUS.Delete_Sanpham_Acquy(id);
                        search_sanpham_acquy(txtSearch_sanpham_acquy.Text);
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message); }
                }
            }
        }

        private void gvSanPham_Acquy_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int id = int.Parse(gvSanPham_Acquy.CurrentRow.Cells["clSanpham_Acquy_id"].Value.ToString());
            
            // xoa ac quy
            if (e.ColumnIndex == 1)
            {
                if ((MessageBox.Show("Bạn muốn xóa ac quy này ??", "Thông Báo", MessageBoxButtons.YesNo)) == DialogResult.Yes)
                {
                    try
                    {
                        AcQuyBUS.Delete_Sanpham_Acquy(id);
                        search_sanpham_acquy(txtSearch_sanpham_acquy.Text);
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message); }
                }
            }

            // update ac quy
            if (e.ColumnIndex == 0)
            {
                Update_Sanpham_Acquy fr = new Update_Sanpham_Acquy();
                fr.Tag = id;
                fr.ShowDialog();

                search_sanpham_acquy(txtSearch_sanpham_acquy.Text);
            }
        }





// San pham: Bo chuyen doi dien--------------------------------------------------------------------------------------------------------------------------------

        private void btnSeacrh_sanpham_Bochuyendoidien_Click(object sender, EventArgs e)
        {
            try
            { 
                string text = txtSeacrh_sanpham_bochuyendoidien.Text;
                gvSanpham_bochuyendoi.DataSource = search_sanpham_bochuyendoidien(text);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        // search san pham: Bo chuyen doi dien
        private List<Sanpham_BochuyendoidienDTO> search_sanpham_bochuyendoidien(string text)
        {
            return BoChuyenDoiDienBUS.search_sanpham_bochuyendoi(text);
        }

        private void txtSeacrh_sanpham_bochuyendoidien_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    string text = txtSeacrh_sanpham_bochuyendoidien.Text;
                    gvSanpham_bochuyendoi.DataSource = search_sanpham_bochuyendoidien(text);
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }

        private bool SanPham_StatusBochuyendoi()
        {
            if (txtSanpham_bcd_congsuat.Text == "Hãy nhập")
            {
                txtSanpham_bcd_congsuat.ForeColor = System.Drawing.Color.Red;
                return false;
            }
            if (txtSanpham_bcd_loai.Text == "Hãy nhập")
            {
                txtSanpham_bcd_loai.ForeColor = System.Drawing.Color.Red;
                return false;
            }
            if (txtSanpham_bcd_giatien.Text == "Hãy nhập")
            {
                txtSanpham_bcd_giatien.ForeColor = System.Drawing.Color.Red;
                return false;
            }

            return true;
        }

        private void btnThem_sanpham_bochuyendoi_Click(object sender, EventArgs e)
        {
            if (SanPham_StatusBochuyendoi())
            {
                Sanpham_BochuyendoidienDTO bochuyen = new Sanpham_BochuyendoidienDTO();
                bochuyen.congsuat = txtSanpham_bcd_congsuat.Text;
                bochuyen.loai = txtSanpham_bcd_loai.Text;
                bochuyen.giatien = float.Parse(txtSanpham_bcd_giatien.Text);

                try
                {
                    BoChuyenDoiDienDAO.Insert_sanpham_bochuyendoi(bochuyen);

                    // khởi tao lai
                    txtSanpham_bcd_congsuat.Text = "Hãy nhập";
                    txtSanpham_bcd_loai.Text = "Hãy nhập";
                    txtSanpham_bcd_giatien.Text = "Hãy nhập";
                    txtSanpham_bcd_congsuat.ForeColor = System.Drawing.Color.DarkGray;
                    txtSanpham_bcd_loai.ForeColor = System.Drawing.Color.DarkGray;
                    txtSanpham_bcd_giatien.ForeColor = System.Drawing.Color.DarkGray;

                    // load lai
                    string text = txtSeacrh_sanpham_bochuyendoidien.Text;
                    gvSanpham_bochuyendoi.DataSource = search_sanpham_bochuyendoidien(text);
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }


        // event text changed----------------------------------------------------------------
        private void txtSanpham_bcd_congsuat_TextChanged(object sender, EventArgs e)
        {
            txtSanpham_bcd_congsuat.ForeColor = System.Drawing.Color.Black;
        }

        private void txtSanpham_bcd_loai_TextChanged(object sender, EventArgs e)
        {
            txtSanpham_bcd_loai.ForeColor = System.Drawing.Color.Black;
        }

        private void txtSanpham_bcd_giatien_TextChanged(object sender, EventArgs e)
        {
            txtSanpham_bcd_giatien.ForeColor = System.Drawing.Color.Black;
        }


        // event leave---------------------------------------------------------------------------------
        private void txtSanpham_bcd_congsuat_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSanpham_bcd_congsuat.Text))
            {
                txtSanpham_bcd_congsuat.Text = "Hãy nhập";
                txtSanpham_bcd_congsuat.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtSanpham_bcd_loai_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSanpham_bcd_loai.Text))
            {
                txtSanpham_bcd_loai.Text = "Hãy nhập";
                txtSanpham_bcd_loai.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        private void txtSanpham_bcd_giatien_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSanpham_bcd_giatien.Text))
            {
                txtSanpham_bcd_giatien.Text = "Hãy nhập";
                txtSanpham_bcd_giatien.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        
        // event click-----------------------------------------------------------------
        private void txtSanpham_bcd_congsuat_Click(object sender, EventArgs e)
        {
            if (txtSanpham_bcd_congsuat.Text == "Hãy nhập")
                txtSanpham_bcd_congsuat.Text = "";
        }

        private void txtSanpham_bcd_loai_Click(object sender, EventArgs e)
        {
            if (txtSanpham_bcd_loai.Text == "Hãy nhập")
                txtSanpham_bcd_loai.Text = "";
        }

        private void txtSanpham_bcd_giatien_Click(object sender, EventArgs e)
        {
            if (txtSanpham_bcd_giatien.Text == "Hãy nhập")
                txtSanpham_bcd_giatien.Text = "";
        }


        // gia tien hai la so--------------------------------------------------------------
        private void txtSanpham_bcd_giatien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
        }

        private void gvSanpham_bochuyendoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                try
                {
                    if ((MessageBox.Show("Bạn muốn xóa bộ kích này ??", "Thông Báo", MessageBoxButtons.YesNo)) == DialogResult.Yes)
                    {
                        int id = int.Parse(gvSanpham_bochuyendoi.CurrentRow.Cells["clid_sanpham_bcd"].Value.ToString());
                        BoChuyenDoiDienBUS.Delete_sanpham_bochuyendoi(id);
                        string text = txtSeacrh_sanpham_bochuyendoidien.Text;
                        gvSanpham_bochuyendoi.DataSource = search_sanpham_bochuyendoidien(text);
                    }
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
        }

        private void gvSanpham_bochuyendoi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int id = int.Parse(gvSanpham_bochuyendoi.CurrentRow.Cells["clid_sanpham_bcd"].Value.ToString());

            // xoa bo chuyen doi
            if (e.ColumnIndex == 1)
            {
                try
                {
                    if ((MessageBox.Show("Bạn muốn xóa bộ kích này ??", "Thông Báo", MessageBoxButtons.YesNo)) == DialogResult.Yes)
                    {
                        BoChuyenDoiDienBUS.Delete_sanpham_bochuyendoi(id);
                        string text = txtSeacrh_sanpham_bochuyendoidien.Text;
                        gvSanpham_bochuyendoi.DataSource = search_sanpham_bochuyendoidien(text);
                    }
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }

            // caop nhap thing tin bo chuyen doi
            if (e.ColumnIndex == 0)
            {
                Update_Sanpham_Bokichdien fr = new Update_Sanpham_Bokichdien();
                fr.Tag = id;
                fr.ShowDialog();

                string text = txtSeacrh_sanpham_bochuyendoidien.Text;
                gvSanpham_bochuyendoi.DataSource = search_sanpham_bochuyendoidien(text);
            }
        }

        private void btnChonMachSac_Click(object sender, EventArgs e)
        {
            if (co)
            {
                frmChonMachSac frm = new frmChonMachSac();
                frm.Tag = _congSuatCuaPinTaoRa;
                frm.ShowDialog();

                DataTable dt = new DataTable();
                TongTienTatCa += HoaDonHelper.tongtien;
                dt = HoaDonHelper.dt;
                lbTienMachSac.Text = HoaDonHelper.tongtien.ToString("###,###,##0.", CultureInfo.InvariantCulture).TrimStart().Replace(".", ",") + " VNĐ";
                gvHD_MS.DataSource = dt;
                dt = null;
                
            }
            else
                MessageBox.Show("Hãy SAVE Trước Khi Chọn Chức Năng!!!!!!");
        }

















































        private void timer1_Tick(object sender, EventArgs e)
        {
            // man hinh bo chuyen doi dien
            if ((txtSanpham_bcd_loai.ForeColor == System.Drawing.Color.DarkGray || txtSanpham_bcd_loai.ForeColor == System.Drawing.Color.Silver) && (txtSanpham_bcd_congsuat.ForeColor == System.Drawing.Color.Silver || txtSanpham_bcd_congsuat.ForeColor == System.Drawing.Color.DarkGray) && (txtSanpham_bcd_giatien.ForeColor == System.Drawing.Color.Silver || txtSanpham_bcd_giatien.ForeColor == System.Drawing.Color.DarkGray))
            {
                if (timer % 3 == 2)
                {
                    txtSanpham_bcd_loai.ForeColor = System.Drawing.Color.DarkGray;
                    txtSanpham_bcd_congsuat.ForeColor = System.Drawing.Color.DarkGray;
                    txtSanpham_bcd_giatien.ForeColor = System.Drawing.Color.DarkGray;
                }
                else
                {
                    txtSanpham_bcd_loai.ForeColor = System.Drawing.Color.Silver;
                    txtSanpham_bcd_congsuat.ForeColor = System.Drawing.Color.Silver;
                    txtSanpham_bcd_giatien.ForeColor = System.Drawing.Color.Silver;
                }
            }
            else
            {
                Sanpham_bokichdien_colorRed();
            }

            // màn hình ac quy
            if ((txtAcquy_giatien.ForeColor == System.Drawing.Color.DarkGray || txtAcquy_giatien.ForeColor == System.Drawing.Color.Silver) && (txtAcquy_soAH.ForeColor == System.Drawing.Color.Silver || txtAcquy_soAH.ForeColor == System.Drawing.Color.DarkGray) && (txtAcquy_tensanpham.ForeColor == System.Drawing.Color.Silver || txtAcquy_tensanpham.ForeColor == System.Drawing.Color.DarkGray))
            {
                if (timer % 3 == 2)
                {
                    txtAcquy_tensanpham.ForeColor = System.Drawing.Color.DarkGray;
                    txtAcquy_soAH.ForeColor = System.Drawing.Color.DarkGray;
                    txtAcquy_giatien.ForeColor = System.Drawing.Color.DarkGray;
                }
                else
                {
                    txtAcquy_tensanpham.ForeColor = System.Drawing.Color.Silver;
                    txtAcquy_soAH.ForeColor = System.Drawing.Color.Silver;
                    txtAcquy_giatien.ForeColor = System.Drawing.Color.Silver;
                }
            }
            else
            {
                Sanpham_acquy_colorRed();
            }

            // màn hình bộ điều khiển sạc
            if ((txtMachSac_HieuSuat1.ForeColor == System.Drawing.Color.DarkGray || txtMachSac_HieuSuat1.ForeColor == System.Drawing.Color.Silver) && (txtMachSac_HieuSuat2.ForeColor == System.Drawing.Color.Silver || txtMachSac_HieuSuat2.ForeColor == System.Drawing.Color.DarkGray) && (txtMachSac_Ample.ForeColor == System.Drawing.Color.Silver || txtMachSac_Ample.ForeColor == System.Drawing.Color.DarkGray) && (txtMachSac_GiaTien.ForeColor == System.Drawing.Color.Silver || txtMachSac_GiaTien.ForeColor == System.Drawing.Color.DarkGray))
            {
                if (timer % 3 == 2)
                {
                    txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.DarkGray;
                    txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.DarkGray;
                    txtMachSac_Ample.ForeColor = System.Drawing.Color.DarkGray;
                    txtMachSac_GiaTien.ForeColor = System.Drawing.Color.DarkGray;
                }
                else
                {
                    txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.Silver;
                    txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.Silver;
                    txtMachSac_Ample.ForeColor = System.Drawing.Color.Silver;
                    txtMachSac_GiaTien.ForeColor = System.Drawing.Color.Silver;
                }
            }
            else
            {
                Sanpham_machdieukhiensac_colorRed();
            }

            // màn hình pin
            if ((txtKichThuocPin.ForeColor == System.Drawing.Color.DarkGray || txtKichThuocPin.ForeColor == System.Drawing.Color.Silver) && (txtVonMaxPin.ForeColor == System.Drawing.Color.Silver || txtVonMaxPin.ForeColor == System.Drawing.Color.DarkGray) && (txtVonMinPin.ForeColor == System.Drawing.Color.Silver || txtVonMinPin.ForeColor == System.Drawing.Color.DarkGray) && (txtAmpeMaxPin.ForeColor == System.Drawing.Color.Silver || txtAmpeMaxPin.ForeColor == System.Drawing.Color.DarkGray) && (txtAmpeMinPin.ForeColor == System.Drawing.Color.Silver || txtAmpeMinPin.ForeColor == System.Drawing.Color.DarkGray))
            {
                if (timer % 3 == 2)
                {
                    txtKichThuocPin.ForeColor = System.Drawing.Color.DarkGray;
                    txtVonMaxPin.ForeColor = System.Drawing.Color.DarkGray;
                    txtVonMinPin.ForeColor = System.Drawing.Color.DarkGray;
                    txtAmpeMaxPin.ForeColor = System.Drawing.Color.DarkGray;
                    txtAmpeMinPin.ForeColor = System.Drawing.Color.DarkGray;
                }
                else
                {
                    txtKichThuocPin.ForeColor = System.Drawing.Color.Silver;
                    txtVonMaxPin.ForeColor = System.Drawing.Color.Silver;
                    txtVonMinPin.ForeColor = System.Drawing.Color.Silver;
                    txtAmpeMaxPin.ForeColor = System.Drawing.Color.Silver;
                    txtAmpeMinPin.ForeColor = System.Drawing.Color.Silver;
                }
            }
            else
            {
                Sanpham_pin_colorRed();
            }



            // xet bien timer
            timer += 1;

            // khoi chay timer1
            Invalidate();
        }

        // hiệu ứng color red và indianred cho màn hình pin
        private void Sanpham_pin_colorRed()
        {
            if (txtKichThuocPin.ForeColor == System.Drawing.Color.Red || txtKichThuocPin.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtKichThuocPin.ForeColor = System.Drawing.Color.IndianRed;
                else txtKichThuocPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtVonMaxPin.ForeColor == System.Drawing.Color.Red || txtVonMaxPin.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtVonMaxPin.ForeColor = System.Drawing.Color.IndianRed;
                else txtVonMaxPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtVonMinPin.ForeColor == System.Drawing.Color.Red || txtVonMinPin.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtVonMinPin.ForeColor = System.Drawing.Color.IndianRed;
                else txtVonMinPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtAmpeMaxPin.ForeColor == System.Drawing.Color.Red || txtAmpeMaxPin.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtAmpeMaxPin.ForeColor = System.Drawing.Color.IndianRed;
                else txtAmpeMaxPin.ForeColor = System.Drawing.Color.Red;
            }
            if (txtAmpeMinPin.ForeColor == System.Drawing.Color.Red || txtAmpeMinPin.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtAmpeMinPin.ForeColor = System.Drawing.Color.IndianRed;
                else txtAmpeMinPin.ForeColor = System.Drawing.Color.Red;
            }
        }

        // hieu ung color red & indianred cho bo dieu khien sac
        private void Sanpham_machdieukhiensac_colorRed()
        {
            if (txtMachSac_HieuSuat1.ForeColor == System.Drawing.Color.Red || txtMachSac_HieuSuat1.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.IndianRed;
                else txtMachSac_HieuSuat1.ForeColor = System.Drawing.Color.Red;
            }
            if (txtMachSac_HieuSuat2.ForeColor == System.Drawing.Color.Red || txtMachSac_HieuSuat2.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.IndianRed;
                else txtMachSac_HieuSuat2.ForeColor = System.Drawing.Color.Red;
            }
            if (txtMachSac_Ample.ForeColor == System.Drawing.Color.Red || txtMachSac_Ample.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtMachSac_Ample.ForeColor = System.Drawing.Color.IndianRed;
                else txtMachSac_Ample.ForeColor = System.Drawing.Color.Red;
            }
            if (txtMachSac_GiaTien.ForeColor == System.Drawing.Color.Red || txtMachSac_GiaTien.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtMachSac_GiaTien.ForeColor = System.Drawing.Color.IndianRed;
                else txtMachSac_GiaTien.ForeColor = System.Drawing.Color.Red;
            }
        }

        // hieu ung color red & indianred cho bo kich dien
        private void Sanpham_bokichdien_colorRed()
        {
            if (txtSanpham_bcd_loai.ForeColor == System.Drawing.Color.Red || txtSanpham_bcd_loai.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtSanpham_bcd_loai.ForeColor = System.Drawing.Color.IndianRed;
                else txtSanpham_bcd_loai.ForeColor = System.Drawing.Color.Red;
            }
            if (txtSanpham_bcd_congsuat.ForeColor == System.Drawing.Color.Red || txtSanpham_bcd_congsuat.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtSanpham_bcd_congsuat.ForeColor = System.Drawing.Color.IndianRed;
                else txtSanpham_bcd_congsuat.ForeColor = System.Drawing.Color.Red;
            }
            if (txtSanpham_bcd_giatien.ForeColor == System.Drawing.Color.Red || txtSanpham_bcd_giatien.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtSanpham_bcd_giatien.ForeColor = System.Drawing.Color.IndianRed;
                else txtSanpham_bcd_giatien.ForeColor = System.Drawing.Color.Red;
            }
        }

        // hiệu ứng color red và indianred cho màn hình ac quy
        private void Sanpham_acquy_colorRed()
        {
            if (txtAcquy_tensanpham.ForeColor == System.Drawing.Color.Red || txtAcquy_tensanpham.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtAcquy_tensanpham.ForeColor = System.Drawing.Color.IndianRed;
                else txtAcquy_tensanpham.ForeColor = System.Drawing.Color.Red;
            }
            if (txtAcquy_soAH.ForeColor == System.Drawing.Color.Red || txtAcquy_soAH.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtAcquy_soAH.ForeColor = System.Drawing.Color.IndianRed;
                else txtAcquy_soAH.ForeColor = System.Drawing.Color.Red;
            }
            if (txtAcquy_giatien.ForeColor == System.Drawing.Color.Red || txtAcquy_giatien.ForeColor == System.Drawing.Color.IndianRed)
            {
                if (timer % 3 == 2)
                    txtAcquy_giatien.ForeColor = System.Drawing.Color.IndianRed;
                else txtAcquy_giatien.ForeColor = System.Drawing.Color.Red;
            }
        }


        
        
        private void btnChonBoKichDien_Click(object sender, EventArgs e)
        {
            if (co)
            {   
                frmChonBoKichDien frm = new frmChonBoKichDien();
               
                frm.Tag = _tongCongSuatTieuThu;
                frm.ShowDialog();

                DataTable dt = new DataTable();
                dt = HoaDonHelper.dt;
                TongTienTatCa += HoaDonHelper.tongtien;
                lbTienBoKichDien.Text = HoaDonHelper.tongtien.ToString("###,###,##0.", CultureInfo.InvariantCulture).TrimStart().Replace(".", ",") + " VNĐ";
                
                
                gvHoaDon_BoKichDien.DataSource = dt;
                dt = null;
            }
            else
                MessageBox.Show("Hãy SAVE Trước Khi Chọn Chức Năng!!!!!!");
        }

        private void btnChonPin_Click_1(object sender, EventArgs e)
        {
            if (co)
            {
                frmLoaiPin frm = new frmLoaiPin();
                frm.Tag = new string[] { _tongCongSuatTieuThu.ToString(), cmbDongPin.Text };
                frm.ShowDialog();
                DataTable dt = new DataTable();
                dt = HoaDonHelper.dt;
                
                gvHD_Pin.DataSource = dt;
                dt = null;
                TongTienTatCa += HoaDonHelper.tongtien;

            }
            else
                MessageBox.Show("Hãy SAVE Trước Khi Chọn Chức Năng!!!!!!");
        }

        private void tabHD_Click(object sender, EventArgs e)
        {

        }

        private void btnCapNhapThamSo_Click(object sender, EventArgs e)
        {
            FrmCapNhatThamSo fr = new FrmCapNhatThamSo();
            fr.ShowDialog();
            load_pin();
        }

        private void groupBox5_Enter(object sender, EventArgs e)
        {

        }

        private void button5_MouseLeave(object sender, EventArgs e)
        {

            // thay đổi hình ảnh và kích thước của button "this"
            this.button5.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.button5.BackgroundImageLayout = ImageLayout.Stretch;
            //this.button1.Width = 133;
            //this.button1.Height = 58;
        }

        private void button5_MouseEnter(object sender, EventArgs e)
        {
            this.button5.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.kkk));
            this.button5.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void button3_MouseEnter(object sender, EventArgs e)
        {
            this.button3.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.kkk));
            this.button3.BackgroundImageLayout = ImageLayout.Stretch;   
        }

        private void button3_MouseLeave(object sender, EventArgs e)
        {
            this.button3.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.button3.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void button2_MouseLeave(object sender, EventArgs e)
        {
            this.button2.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.button2.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void button2_MouseEnter(object sender, EventArgs e)
        {
            this.button2.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.kkk));
            this.button2.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {

            this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.kkk));
            this.button1.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {

            this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.button1.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnCapNhapThamSo_MouseLeave(object sender, EventArgs e)
        {

            this.btnCapNhapThamSo.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.btnCapNhapThamSo.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnCapNhapThamSo_MouseEnter(object sender, EventArgs e)
        {

            this.btnCapNhapThamSo.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.kkk));
            this.btnCapNhapThamSo.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnChonPin_MouseEnter(object sender, EventArgs e)
        {

            this.btnChonPin.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.kkk));
            this.btnChonPin.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnChonPin_MouseLeave(object sender, EventArgs e)
        {

            this.btnChonPin.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.btnChonPin.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnChonMachSac_MouseEnter(object sender, EventArgs e)
        {

            this.btnChonMachSac.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.kkk));
            this.btnChonMachSac.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnChonMachSac_MouseLeave(object sender, EventArgs e)
        {

            this.btnChonMachSac.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.btnChonMachSac.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void button4_MouseEnter(object sender, EventArgs e)
        {

            this.button4.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.kkk));
            this.button4.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void button4_MouseLeave(object sender, EventArgs e)
        {

            this.button4.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.button4.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnChonBoKichDien_MouseLeave(object sender, EventArgs e)
        {

            this.btnChonBoKichDien.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.acb));
            this.btnChonBoKichDien.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void btnChonBoKichDien_MouseEnter(object sender, EventArgs e)
        {

            this.btnChonBoKichDien.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.kkk));
            this.btnChonBoKichDien.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            tabMenu.SelectedTab = tabSP;
            tabControl1.SelectedTab = tabPage1;
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            tabMenu.SelectedTab = tabSP;
            tabControl1.SelectedTab = tabPage3;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tabMenu.SelectedTab = tabSP;
            tabControl1.SelectedTab = tabPage2;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            tabMenu.SelectedTab = tabSP;
            tabControl1.SelectedTab = tabPage4;
        }


        string TenKH, DiaChi, SoDT;

        private void btn_ThemKH_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtSoDT.Text == "" || txtTenKH.Text == "" || rtDiaChi.Text == "")
                {
                    MessageBox.Show("Hãy Nhập Đủ Thông Tin");
                }
                else
                {
                    TenKH = txtTenKH.Text;
                    DiaChi = rtDiaChi.Text;
                    SoDT = txtSoDT.Text;

                    // gan thông tin khách hàng
                    lbHoaDon_HoTenKH.Text = TenKH;
                    lbHD_diachi.Text = DiaChi;
                    lbHD_dienthoaikh.Text = SoDT;
                    lbHD_diachi.MaximumSize = grbthongtinkhachhang.Size - new Size(150,32);

                    dgThemKH.Rows.Add(TenKH,DiaChi,SoDT);
                    txtTenKH.Text = "";
                    rtDiaChi.Text = "";
                    txtSoDT.Text = "";
                    gbKH.Visible = false;
                    gbTTSP.Visible = true;
                }
                
                 
            }
            catch (Exception)
            {
                MessageBox.Show("Hãy Nhập Đủ Thông Tin");
            }
          
        }

     

        private void dgThemSP_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 6)
            {
                if (dgThemSP.SelectedRows.Count > 0)
                {
                    foreach (DataGridViewRow r in dgThemSP.SelectedRows)
                    {
                        if (r.Index != dgThemSP.NewRowIndex)
                        {
                            dgThemSP.Rows.Remove(r);
                        }
                    }
                    CapNhatCongSuatTieuThu();
                }

               
                if (txtSoGioNang.Text != "")
                    btnTinh_Click(sender, e);
            }
        }

        private void dgThemSP_DoubleClick_1(object sender, EventArgs e)
        {
            DataGridViewRow row_CapNhat = dgThemSP.SelectedRows[0];
            FrmCaphatCS frm = new FrmCaphatCS();
            frm.Tag = row_CapNhat;
            frm.ShowDialog();


            if (flag_CapNhat)
            {
                //Tat form cap nhat
                string[] _arrNewData = FrmCaphatCS._arrNewData;
                row_CapNhat.Cells[0].Value = _arrNewData[0];
                row_CapNhat.Cells[1].Value = _arrNewData[1];
                row_CapNhat.Cells[2].Value = _arrNewData[2];
                row_CapNhat.Cells[3].Value = _arrNewData[3];
                row_CapNhat.Cells[4].Value = _arrNewData[4];
                row_CapNhat.Cells[5].Value = int.Parse(_arrNewData[1]) * int.Parse(_arrNewData[2]) * int.Parse(_arrNewData[3]) + int.Parse(_arrNewData[4]);

            }

            CapNhatCongSuatTieuThu();



            if (txtSoGioNang.Text != "")
                btnTinh_Click(sender, e);
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            co = true;
            
            if (gbKH.Visible == false && flag == true && flag1 == true)
            {
                MessageBox.Show("Đơn Hàng Thành Công,Hãy Chọn Các Thiết Bị Đi Kèm!!");
            }
            else
                MessageBox.Show("Hãy điền đầy đủ thông tin!!!!!!");
        }
        int dem = 0;
        private void tabMenu_Selected(object sender, TabControlEventArgs e)
        {
            if (tabMenu.SelectedIndex == 2 && co == true)
            {
                
                if (dem == 0)
                {
                    TongTienTatCa += double.Parse(txtTienPin.Text);
                    dem++;
                }

                lbTienPin.Text = txtTienPin.Text + " VNĐ";
                //if (cmbDongPin.SelectedIndex == 0)
                //{
                //    //lbLoaiPin.Text = "MONO";
                //}
                //else
                //{
                //    //lbLoaiPin.Text = "POLY";
                //}

                label16.Text = TongTienTatCa.ToString("###,###,##0.", CultureInfo.InvariantCulture).TrimStart().Replace(".", ",") + " VNĐ";

            }
            else if (tabMenu.SelectedIndex == 2 && co == false)
            {
                MessageBox.Show("Hãy Nhập 1 Đơn Hàng");
                tabMenu.SelectedIndex = 0;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
           
            txtTenKH.Text = "";
            rtDiaChi.Text = "";
            txtSoDT.Text = "";
            gbKH.Visible = true;
            gbTTSP.Visible = false;
            txtTenSP.Text = "";
            txtCS.Text = "";
            for (int i = 0; i < dgThemKH.Rows.Count; i++)
            {
                dgThemKH.Rows.RemoveAt(i);
            }
            for (int i = 0; i < dgThemSP.Rows.Count; i++)
            {
                dgThemSP.Rows.RemoveAt(i);
            }
            txtThoiGian.Text = "";
            txtSoLuong.Text = "";
            txtCSHaoHut.Text = "";
            gbTinhCongSuatPin.Enabled = false;
            txtTienPin.Text = "";
            txtTongCSPin.Text = "";
            txtTongCS.Text = "";

        }

        private void txtSoDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Mời bạn nhập số");

            }
        }


        List<HoaDon_ThietBi> list_HoaDon_ThietBi = new List<HoaDon_ThietBi>();
        private void button6_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Excel file *.xls|*.xls";
            save.FileName = "HoaDonXuat";
            
            if (save.ShowDialog() == DialogResult.OK)
            {
                ThongTinXuatExcel thongtin = new ThongTinXuatExcel();
                thongtin.HoTen = lbHoaDon_HoTenKH.Text;
                thongtin.DiaChi = lbHD_diachi.Text;
                thongtin.DienThoai = lbHD_dienthoaikh.Text;
                thongtin.TienBoKichDien = lbTienBoKichDien.Text;
                thongtin.TienMachSac = lbTienMachSac.Text;
                thongtin.TienPin = lbTienPin.Text;
                thongtin.TienAcQuy = lbTienAcQuy.Text;
                thongtin.TongTien = label16.Text;

                string partSave = save.FileName.ToString();
                xuatExcel_HoaDon_Object(thongtin,partSave);
            }
        }

        private void xuatExcel_HoaDon_Object(ThongTinXuatExcel thongtin,string partSave)
        {
            COMExcel.Application exApp = new COMExcel.Application();
            string partTemp = Application.StartupPath;
            string templatePart = Application.StartupPath.Substring(0,partTemp.Length - 10) + @"\Resources\HoaDonKhachHang.xls";

            // khoi tao file excel tu template HoaDonKhachHang.xls
            COMExcel.Workbook exBook = exApp.Workbooks.Open(templatePart,
                    0, false, 5, "", "", false, COMExcel.XlPlatform.xlWindows, "",
                    true, false, 0, true, false, false);

            // lay sheest 1
            COMExcel.Worksheet exSheet = (COMExcel.Worksheet)exBook.Worksheets[1];
            
            // set name cho sheest dc chon: activate
            exSheet.Activate();
            exSheet.Name = "Hoa Don Thanh Toan";

            // ghi dữ liệu vào ô cần ghi
            COMExcel.Range hoten = (COMExcel.Range)exSheet.Cells[6, 2];
            hoten.Value = thongtin.HoTen;
            COMExcel.Range dienthoai = (COMExcel.Range)exSheet.Cells[7, 2];
            dienthoai.Value = thongtin.DienThoai;
            COMExcel.Range diachi = (COMExcel.Range)exSheet.Cells[8, 2];
            diachi.Value = thongtin.DiaChi;

            COMExcel.Range tienBKD = (COMExcel.Range)exSheet.Cells[5, 7];
            tienBKD.Value = thongtin.TienBoKichDien;
            COMExcel.Range tienMS = (COMExcel.Range)exSheet.Cells[6, 7];
            tienMS.Value = thongtin.TienMachSac;
            COMExcel.Range tienPin = (COMExcel.Range)exSheet.Cells[7, 7];
            tienPin.Value = thongtin.TienPin;
            COMExcel.Range tienAQ = (COMExcel.Range)exSheet.Cells[8, 7];
            tienAQ.Value = thongtin.TienAcQuy;
            COMExcel.Range tongTien = (COMExcel.Range)exSheet.Cells[10, 2];
            tongTien.Value = thongtin.TongTien;

            for (int i = 0; i < gvThietBi.Rows.Count; i++)
            {
                string _tenSP = gvThietBi.Rows[i].Cells[0].Value.ToString();
                COMExcel.Range tenSP = (COMExcel.Range)exSheet.Cells[13 + i, 2];
                tenSP.Value = _tenSP;

                string _congsuat = gvThietBi.Rows[i].Cells[1].Value.ToString();
                COMExcel.Range congsuat = (COMExcel.Range)exSheet.Cells[13 + i, 3];
                congsuat.Value = _congsuat;

                string _thoigian = gvThietBi.Rows[i].Cells[2].Value.ToString();
                COMExcel.Range thoigian = (COMExcel.Range)exSheet.Cells[13 + i, 4];
                thoigian.Value = _thoigian;

                string _soluong = gvThietBi.Rows[i].Cells[3].Value.ToString();
                COMExcel.Range soluong = (COMExcel.Range)exSheet.Cells[13 + i, 5];
                soluong.Value = _soluong;

                string _csHaohut = gvThietBi.Rows[i].Cells[4].Value.ToString();
                COMExcel.Range csHaohut = (COMExcel.Range)exSheet.Cells[13 + i, 6];
                csHaohut.Value = _csHaohut;

                string _tongCS = gvThietBi.Rows[i].Cells[5].Value.ToString();
                COMExcel.Range tongCS = (COMExcel.Range)exSheet.Cells[13 + i, 7];
                tongCS.Value = _tongCS;
            }

            // Giãn cột
            //r.Columns.AutoFit();

            // Hiển thị chương trình excel
//            exApp.Visible = true;
            // Ẩn chương trình
//            exApp.Visible = false;

            // Save file
            exBook.SaveAs(partSave, COMExcel.XlFileFormat.xlWorkbookNormal,
                            null, null, false, false,
                            Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
                            false, false, false, false, false);

            exBook.Close(false, false, false);
            exApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(exBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(exApp);
        }














    }
}
